<?php

namespace app\common\model;

use app\common\model\BaseModel;


class WorkerInfo extends BaseModel
{

    

    

    // 表名
    protected $name = 'worker_info';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'create_time_text',
        'update_time_text',
        'audit_time_text',
        'license_arr'
    ];

    protected static function onBeforeUpdate($row)
    {
        $changed = $row->getChangedData();

        if (isset($changed['pzy_status'])||isset($changed['phy_status'])) {

            $data=[];

            if($row['pzy_status']=='unpaid'||$row['phy_status']=='unpaid'){
                $check_deposit=Deposit::where(['user_id'=>$row['user_id'],'status'=>1])->find()?:[];
                if($check_deposit){
                    if($row['pzy_status']=='unpaid'){
                        $row['pzy_status']='normal';
                        $data['pzy_status']='normal';
                    }
                    if($row['phy_status']=='unpaid'){
                        $row['phy_status']='normal';
                        $data['phy_status']='normal';
                    }
                    User::update($data,['id'=>$row['user_id']]);
                }
            }
            else{
                User::update(['pzy_status'=>$row['pzy_status'],'phy_status'=>$row['phy_status']],['id'=>$row['user_id']]);
            }

        }
    }

    public function getGenderList()
    {
        return ['男' => '男', '女' => '女'];
    }


    public function getStatusList()
    {
        return [
            'normal'=>'正常',
            'trash'=>'停用',
            'deleted'=>'已删除',
            'reviewing'=>'审核中',
            'refused'=>'审核拒绝',
            'unpaid'=>'未缴纳保证金',
            'none'=>'未申请'
        ];
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAuditTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['audit_time']) ? $data['audit_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getLicenseArrAttr($value, $data)
    {
        return explode(',',$data['license'] ?? '');
    }


    public function setLicenseAttr($value)
    {
        $value = $value ? $value : '';
        $arr= explode(',',$value);

        foreach ($arr as &$a){
            $a=cdnurl($a,true);
        }

        return implode(',',$arr);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAuditTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id')->joinType('LEFT');
    }

}
